//
//  SEGNewBusinessCircleProductDetailsViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/28.
//

#import "SEGNewBusinessCircleProductDetailsViewModel.h"
#import "BusinessCircleRequestManager.h"

@interface SEGNewBusinessCircleProductDetailsViewModel ()
@property (nonatomic, strong) NSString *sid;
@end

@implementation SEGNewBusinessCircleProductDetailsViewModel
- (instancetype)initWithSid:(NSString *)sid {
    if (self = [super init]) {
        _sid = sid;
        [self seg_creatDetailsDataCommand];
    }
    return self;
}

- (void)seg_creatDetailsDataCommand {
    @weakify(self);
    self.dataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            if (!self.sid) {
                [subscriber sendCompleted];
                return nil;
            }
            self.sid = ValueToString(self.sid);
            if (self.sid.length == 0) {
                [subscriber sendCompleted];
                return nil;
            }
            
            WeakSelf;
            [BusinessCircleRequestManager requestPrivilegeDetaillWithItemSid:self.sid finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                NSDictionary *dict = result;
                //数据返回正常
                if (isSuccessed) {
                    NSDictionary *data = [dict objectForKey:@"data"];
                    self.productModel = [SEGMerchantProductModel yy_modelWithDictionary:data];
                    NSLog(@"%@",data);
//                    SEGMerchantProductModel *productModel = [SEGMerchantProductModel yy_modelWithDictionary:data];
//
//                    NSMutableArray<SEGMerchantProductDisplayModel *> *firstSectionDisplayModels = [NSMutableArray array];
//                    [firstSectionDisplayModels addObject:[SEGMerchantProductDisplayModel modelWithType:SEGMerchantProductDisplayModelTypeTitle firstText:productModel.merchantInfo.name secondText:productModel.name]];
//
//                    if (productModel.productDescription.length > 0) {
//                        [firstSectionDisplayModels addObject:[SEGMerchantProductDisplayModel modelWithType:SEGMerchantProductDisplayModelTypeNormalText firstText:productModel.productDescription secondText:nil]];
//                    }
//
//                    NSString *timeString = [NSString stringWithFormat:@"有效期：%@到%@",productModel.shortStartTime,productModel.shortEndTime];
//                    [firstSectionDisplayModels addObject:[SEGMerchantProductDisplayModel modelWithType:SEGMerchantProductDisplayModelTypeDetail firstText:productModel.remark secondText:timeString]];
//
//                    NSMutableArray<SEGMerchantProductDisplayModel *> *secondSectionDisplayModels = [NSMutableArray array];
//                    [secondSectionDisplayModels addObject:[SEGMerchantProductDisplayModel modelWithType:SEGMerchantProductDisplayModelTypeNormalText firstText:@"商户详情" secondText:nil]];
//                    [secondSectionDisplayModels addObject:[SEGMerchantProductDisplayModel modelWithType:SEGMerchantProductDisplayModelTypeLocation firstText:productModel.merchantInfo.address secondText:nil]];
//                    [secondSectionDisplayModels addObject:[SEGMerchantProductDisplayModel modelWithType:SEGMerchantProductDisplayModelTypePhone firstText:productModel.merchantInfo.tel1 secondText:nil]];
//
                    NSString *pic1 = ValueToString([data objectForKey:@"pic1"]);
                    NSString *pic2 = ValueToString([data objectForKey:@"pic2"]);
                    NSString *pic3 = ValueToString([data objectForKey:@"pic3"]);
                    NSString *pic4 = ValueToString([data objectForKey:@"pic4"]);
                    NSString *pic5 = ValueToString([data objectForKey:@"pic5"]);
                    NSMutableArray *tempImageArray = [NSMutableArray array];
                    if (pic1.length > 0) {
                        [tempImageArray addObject:pic1];
                    }
                    if (pic2.length > 0) {
                        [tempImageArray addObject:pic2];
                    }
                    if (pic3.length > 0) {
                        [tempImageArray addObject:pic3];
                    }
                    if (pic4.length > 0) {
                        [tempImageArray addObject:pic4];
                    }
                    if (pic5.length > 0) {
                        [tempImageArray addObject:pic5];
                    }

                    weakSelf.imagesArray = [tempImageArray copy];

                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                } else {
                    [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                }
            }];
        
            return nil;
        }];
    }];
}
@end
